package com.example.tbag.healthcare.Activity;

import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import com.example.tbag.healthcare.Adapter.MainListAdapter;
import com.example.tbag.healthcare.Application.MyApplication;
import com.example.tbag.healthcare.Model.Person;
import com.example.tbag.healthcare.R;
import com.example.tbag.healthcare.Util.CallPhoneUtil;
import com.example.tbag.healthcare.Util.DatabaseHelper;
import com.example.tbag.healthcare.Util.TimeUtil;
import com.example.tbag.healthcare.Util.ToastUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class MainActivity extends AppCompatActivity {
    private int id;
    @BindView(R.id.list)
    ListView listView;
    @BindView(R.id.left_btn)
    Button button;
    @BindView(R.id.text)
    TextView textView;
    private int text1 =0;
    private int text2 = 0;
    
    
    private DatabaseHelper helper = new DatabaseHelper(this);
    private List<Person> persons = new ArrayList<>();
    private MainListAdapter adapter;

    @OnClick(R.id.left_btn)
    public void insert() {
        this.startActivity(new Intent(this, AddPersonActivity.class));
    }
    
    @OnClick(R.id.left_btn1)
    public void search(){
        Intent intent = new Intent(this,SearchActivity.class);
        intent.putExtra("id",id);
        startActivity(intent);
    }


    public void getPersons() {
        SQLiteDatabase db = helper.getReadableDatabase();
        Cursor cursor = db.rawQuery("select * from people", null);
        if (cursor != null && cursor.moveToFirst()) {
            persons.add(getPerson(cursor));
            while (cursor.moveToNext()) {
                persons.add(getPerson(cursor));
            }
            cursor.close();
            db.close();
        }
    }

    public Person getPerson(Cursor cursor) {
        int id = cursor.getInt(cursor.getColumnIndex("id"));
        String name = cursor.getString(cursor.getColumnIndex("name"));
        String type = cursor.getString(cursor.getColumnIndex("type"));
        String sex = cursor.getString(cursor.getColumnIndex("sex"));
        String age = cursor.getString(cursor.getColumnIndex("age"));
        String bedNum = cursor.getString(cursor.getColumnIndex("bedNum"));
        Person person = new Person(id, name, type, sex, age, bedNum);
        return person;
    }


    @OnClick(R.id.right_btn)
    public void callForHelp() {
        CallPhoneUtil.call(this);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        id = getIntent().getIntExtra("id",0);
        if(id==0){
            button.setVisibility(View.VISIBLE);
        }
        if(id ==2){
            textView.setVisibility(View.VISIBLE);
            queryMessage();
        }
        ButterKnife.bind(this);
        initListView();
    }

    private void queryMessage() {
        SQLiteDatabase db = helper.getReadableDatabase();
        Cursor cursor = db.query("people",null,"type = ?",new String[]{MyApplication.type},null,null,null);
        if (cursor != null && cursor.moveToFirst()) {
            queryDrag(cursor.getInt(cursor.getColumnIndex("id")));
            while (cursor.moveToNext()) {
                queryDrag(cursor.getInt(cursor.getColumnIndex("id")));
            }
            cursor.close();
            db.close();
        }
        String messasge = MyApplication.name+"一共完成了输液"+text1+"次，"+"一共完成了给药了"+text2+"次";
        textView.setText(messasge);
    }
    
    private void queryDrag(int id){
        Cursor cursor = helper.getWritableDatabase().query("drag",null,"id=? and time =? and  done =? and cure = ?",
                new String[]{id+"", TimeUtil.getDate(),"完成","输液"},null,null,null);
        text1+=cursor.getCount();
        Cursor cursor1 = helper.getWritableDatabase().query("drag",null,"id=? and time =? and  done =? and cure = ?",
                new String[]{id+"", TimeUtil.getDate(),"完成","给药"},null,null,null);
        text2+=cursor1.getCount();
    }
    

    @Override
    protected void onResume() {
        super.onResume();
        persons.clear();
        getPersons();
        adapter.notifyDataSetChanged();
    }

    private void initListView() {
        adapter = new MainListAdapter(persons, this,id);
        listView.setAdapter(adapter);
        if(id!=2&&id!=3) {
            listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
                @Override
                public boolean onItemLongClick(AdapterView<?> parent, View view, final int position, long id) {
                    //定义AlertDialog.Builder对象，当长按列表项的时候弹出确认删除对话框  
                    AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
                    builder.setMessage(getMessage());
                    builder.setTitle("提示");
                    //添加AlertDialog.Builder对象的setPositiveButton()方法  
                    builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            delete(position);
                        }
                    });
                    builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {

                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                        }
                    });
                    builder.create().show();
                    return false;
                }
            });
        }
    }

    private void delete(int position) {
        switch (id){
            case 0:
                delete(persons.get(position));
                persons.remove(position);
                adapter.notifyDataSetChanged();
                ToastUtil.show(getBaseContext(), "该病人已出院");
                break;
            case 1:
                helper.getWritableDatabase().delete("human", "id = ?", new String[]{persons.get(position).id + ""});
                ToastUtil.show(getBaseContext(), "该病人体征表已删除");
                break;
        }
    }

    public String  getMessage(){
        switch (id){
            case 0:
                return "确认该病人已经出院并且隐藏所有信息么？";
            case 1:
                return "确认删除该病人体征表么？";
        }
        return "";
    }

    private void delete(Person person) {
        helper.getWritableDatabase().delete("people", "id = ?", new String[]{person.id + ""});
        helper.getWritableDatabase().delete("human", "id = ?", new String[]{person.id + ""});
        helper.getWritableDatabase().delete("drag", "id = ?", new String[]{person.id + ""});
    }
}
